<?php
/**
 * 帮助管理
 *
 * @category     Shuxun Cms
 * @date         2015年10月18日
 * @author       Shuxun Team <kaiping.jiang@scshux.com>
 * @file         HelpController.php
 */
namespace Scshux\Backend\Controllers;
use Scshux\Mvc\SModel;
use Scshux\Mvc\SQuery;
use Scshux\Utils\Tree;
use Scshux\Utils\Time;
use Scshux\Utils\Uploader;
class HelpController extends  ControllerBase
{

	/**
	 * 帮助列表
	 */
	public  function indexAction()
	{
		$is_ajax  = $this->request->get('is_ajax','int',0);
		$act  = $this->request->get('act');
		if($act == 'remove'){
			$this->_removeHelp();
		}
		$dataList = $this->_getHelpList();
		$this->view->setVar('dataList', $dataList);
		if($is_ajax){

			$this->view->setVar('full_page',0);
			$this->view->start();
			$this->view->render('help','index');
			$this->view->finish();
			$content = $this->view->getContent();
			$data = array(
					'content' => $content,
					'filter'  => $dataList->filter,
					'totalPage' => $dataList->totalPage,
					'recordCount'=>$dataList->totalSize,
					'currentPage'=>$dataList->currentPage,
					'extended'   =>$dataList->extended
			);
			$this->sendResult(true,'',$data);
		}else{
			$this->view->setVar('categoryHtml', $this->getCategoriesHtml());
			$this->view->setVar('full_page',1);
		}
	}

	/**
	 * 帮助编辑操作
	 */
	public  function  editAction()
	{
		$id = $this->request->get('id','int',0);
		if($this->request->isPost()){
			//加入语言
			$_POST['site_id'] = $this->getCore()->getSiteID();
			$postData = $_POST;
			$postData['content'] = stripcslashes($postData['content']);
			$helpModel = new SModel('help');

			if($_FILES['banner']){
				$uploaderObj = new Uploader();
				$postData['banner'] = $uploaderObj->upload($_FILES['banner']);
				if(empty($postData['banner'] )){
					unset($postData['banner'] );
				}
			}

			$postData['pics'] = join(';', $postData['pics']);

			$timezone = array();
			foreach ($postData['timezone']['time'] as $key => $val)
			{
				$timezone[$val] = $postData['timezone']['content'][$key];
			}

			if($timezone){
				$postData['timezone'] = serialize($timezone);
			}
			if($id>0){
				$helpModel->setData($postData);
				$helpModel->update('id='.$id);
			}else{
				$postData['created'] = Time::gmtime();
				unset($postData['id']);
				$helpModel->setData($postData);
				$helpModel->add();
			}
			$this->redirect('help');
		}else{
			if($id>0){
				$helpModel = new SModel('help');
				$item = $helpModel->getObj('id='.$id);
				$this->view->setVar('item', $item);
				if($item->pics){
					$pics = explode(';', $item->pics);
					$this->view->setVar('imgitems', $pics );
				}
				if($item->timezone){
					$item->timezone = unserialize($item->timezone);
				}
			}
			$this->view->setVar('categoryHtml', $this->getCategoriesHtml());
		}
	}

	/**
	 * 帮助分类列表
	 */
	public function   category_listAction()
	{
		$is_ajax  = $this->request->get('is_ajax','int',0);
		$dataList = $this->_getCategoryDataList();
		$this->view->setVar('dataList', $dataList);
		if($is_ajax){

			$this->view->setVar('full_page',0);
			$this->view->start();
			$this->view->render('help','category_list');
			$this->view->finish();
			$content = $this->view->getContent();
			$data = array(
					'content' => $content,
					'filter'  => $dataList->filter,
					'totalPage' => $dataList->totalPage,
					'recordCount'=>$dataList->totalSize,
					'currentPage'=>$dataList->currentPage,
					'extended'   =>$dataList->extended
			);
			$this->sendResult(true,'',$data);
		}else{
			$this->view->setVar('full_page',1);
		}

	}

	/**
	 * 帮助分类编辑
	 */
	public function   category_editAction()
	{
		$id = $this->request->get('id','int',0);
		if($this->request->isPost()){
			$categoryModel = new SModel('help_category');
			
			//加入语言
			$_POST['site_id'] = $this->getCore()->getSiteID();
			$postData = $_POST;

			if($_FILES['banner']){
				$uploaderObj = new Uploader();
				$postData['banner'] = $uploaderObj->upload($_FILES['banner']);
				if(empty($postData['banner'] )){
					unset($postData['banner'] );
				}
			}

			if($id>0){
				$categoryModel->setData($postData);
				$categoryModel->update('id='.$id);
			}else{
				$postData['created'] = Time::gmtime();
				unset($postData['id']);
				$categoryModel->setData($postData);
				$categoryModel->add();
			}
			return $this->redirect('help/category_list');
		}else{
			if($id>0){
				$categoryModel = new SModel('help_category');
				$item = $categoryModel->getObj('id='.$id);
				$this->view->setVar('item', $item);
				$this->view->setVar('categoryHtml', $this->getCategoriesHtml($item->parent_id,$id));
			}else{
				$this->view->setVar('categoryHtml', $this->getCategoriesHtml(0));
			}
		}
	}

	/**
	 * 删除分类
	 */
	public  function  category_delAction()
	{
		$id = $this->request->get('id','int',0);
		if($id>0){
			$categoryModel = new SModel('help_category');
			$categoryModel->del("id='{$id}' or parent_id='{$id}'");

			$helpModel = new SModel('help');
			$helpModel->del("cat_id='{$id}'");
		}
		return $this->redirect('help/category_list');
	}

	/**
	 * 删除信息
	 */
	protected  function _removeHelp()
	{
		$id = $this->request->get('id','int',0);
		$helpModel = new SModel('help');
		$helpModel->del('id='.$id);
	}

  /**
   * 获取用户信息
   */
   protected  function _getHelpList()
   {
	   	$filter = array();

	   	$page = $this->request->get('page','int',1);

	   	$where = '1=1';
	   	
	   	//新增语言过滤
	   	$site_id = $this->getCore()->getSiteID();
	   	$where .= ' and h.site_id = '.$site_id;

	   	if(!empty($_REQUEST['name'])){
	   		$where .= ' and h.`name` like "%'.$_REQUEST['name'].'%"';
	   		$filter['name'] = $_REQUEST['name'];
	   	}

	   	if(!empty($_REQUEST['cat_id'])){
	   		$where .= ' and h.cat_id ='.intval($_REQUEST['cat_id']);
	   		$filter['cat_id'] = $_REQUEST['cat_id'];
	   	}
	   	
	   	$helpQuery = new SQuery('help as h');
	   	$helpQuery->join = ' left join help_category as c on h.cat_id=c.id';
	   	$helpQuery->fields = 'h.*,c.name as cat_name';
	   	$helpQuery->page =  $page;
	   	$helpQuery->where = $where;
	   	$helpQuery->order = 'h.sort desc,h.id desc';
	   	$helpQuery->pagesize = 12;

	   	$items = $helpQuery->find();
	   	$dataList = new \stdClass();
	   	$dataList->totalPage = $helpQuery->paging->getTotalPage();
	   	$dataList->currentPage = $helpQuery->paging->getIndex();
	   	$dataList->items = $items;
	   	$dataList->totalSize = $helpQuery->paging->getTotalSize();
	   	$dataList->filter = $filter;

	   	return  $dataList;
   }


	/**
	 * 分类列表数据
	 */
	protected   function  _getCategoryDataList()
	{
		$filter = array();
		$items = $this->getCategoriesTree();
		foreach ($items as $key => $item)
		{
			$items[$key] = (object)$item;
		}
		$dataList = new \stdClass();
		$dataList->totalPage = 1;
		$dataList->currentPage = 1;
		$dataList->items = $items;
		$dataList->totalSize = count($items);
		$dataList->filter = $filter;
		return  $dataList;
	}

	/**
	 * 获取所有分类html
	 */
	protected  function  getCategoriesHtml($id = 0,$outId=0)
	{
		$categoryModel = new SModel('help_category as h');
		$categoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
		$where = ' h.site_id = '.$site_id;
		
	    $items = $categoryModel->query($where,'*','h.sort desc,h.id','DESC');
	    $treeObj = new Tree($items);
	    return  $treeObj->getHtmlOptions($id,$outId);
	}


	/**
	 * 获取所有分类树
	 */
	protected  function  getCategoriesTree($id = 0)
	{
		$categoryModel = new SModel('help_category as h');
		$categoryModel->setFetchMode(\PDO::FETCH_ASSOC);
		
		//新增语言过滤
		$site_id = $this->getCore()->getSiteID();
		$where = ' h.site_id = '.$site_id;
		
		$items = $categoryModel->query($where,'*','h.sort desc,h.id','DESC');
		$treeObj = new Tree($items);
		return  $treeObj->unlimitedForLevel('&nbsp;&nbsp;',$id);
	}
}